﻿using System;
using System.Data;
using System.Web.UI;
using BLL;
using ET;
using Ext.Net;
using HTML;
using Roles;

namespace ERP.Modules
{
    public partial class Province : UserControl
    {
        private readonly Province_BLL clsProvince = new Province_BLL();
        private readonly Domain_BLL _clsDomain = new Domain_BLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                CheckPermisstion();
                GetData();
                Domain();
                ClearTextBox();
                GetComboBox();
                btnEdit.Disable(true);
                btnDel.Disable(true);
                btnExport.Disable(true);
            }
        }

        //-------Store-Datasource-----------------
        protected void StoreProvince_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearch.Text = string.Empty;
            SelectBoxDomainProvince.Text = string.Empty;
            GetData();
            Domain();
        }

        //--------Buttom-Main------------------------
        protected void btnAddNew_Click(object sender, DirectEventArgs e)
        {
            GetComboBox();
            ClearTextBox();
            WindowsProvince.Show();
        }

        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            var sm = grProvince.GetSelectionModel() as CheckboxSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                GetComboBox();
                ClearTextBox();
                DataDetail(int.Parse(row.RecordID));
                WindowsProvince.Show();
                sm.UpdateSelection();
            }
        }

        protected void btnDel_Click(object sender, DirectEventArgs e)
        {
            var sm = grProvince.GetSelectionModel() as CheckboxSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                try
                {
                    Province_BLL.DeleteProvince(int.Parse(row.RecordID));
                }
                catch (Exception)
                {
                    clsFormat.Alert_ERROR("Thông Báo",
                        "Tồn tại tài liệu con, không xoá được vui lòng xoá tài liệu con trước !");
                }
            }
            GetData();
        }
        protected void SelectBoxDomainProvince_Change(object sender, DirectEventArgs e)
        {
            GetData();
        }
        //--------Buttom-Popup------------------------
        protected void btnSavePopUp_Click(object sender, DirectEventArgs e)
        {
            if (txtProvince_ID.Text == string.Empty)
            {
                InsertProvince();
            }
            else
            {
                if (CheckPermisstionDobclick())
                {
                    HTML.clsFormat.Alert_ERROR("Thông Báo", "Bạn không được cấp quyền để sửa đổi !");
                }
                else
                {
                    UpdateProvince();
                }
            }
        }

        protected void btnCancelPopUp_Click(object sender, DirectEventArgs e)
        {
            WindowsProvince.Close();
        }

        //----------Click-------------------
        protected void ProvinceDblClick(object sender, DirectEventArgs e)
        {
            GetComboBox();
            ClearTextBox();
            int Province_Id = int.Parse(e.ExtraParams["Province_Id"]);
            DataDetail(Province_Id);
            WindowsProvince.Show();
        }

        #region[Method]

        protected void GetData()
        {
            if (SelectBoxDomainProvince.Text == string.Empty)
            {
                DataTable dtProvince = clsProvince.BindGirdProvince(string.Empty);
                if (dtProvince != null && dtProvince.Rows.Count > 0)
                {
                    StoreProvince.DataSource = dtProvince;
                    StoreProvince.DataBind();
                }
                else
                {
                    grProvince.GetStore().RemoveAll();
                }
            }
            else
            {
                DataTable dtProvince =
                    clsProvince.GetProvinceFillter(int.Parse(SelectBoxDomainProvince.SelectedItem.Value));
                if (dtProvince != null && dtProvince.Rows.Count > 0)
                {
                    StoreProvince.DataSource = dtProvince;
                    StoreProvince.DataBind();
                }
                else
                {
                    grProvince.GetStore().RemoveAll();
                }
            }
        }

        protected void DataDetail(int id)
        {
            DataTable dtProvince = clsProvince.BindGirdProvinceDetail(id);
            if (dtProvince != null && dtProvince.Rows.Count > 0)
            {
                txtProvince_ID.Text = dtProvince.Rows[0]["Province_ID"].ToString();
                txtProvince_Alias.Text = dtProvince.Rows[0]["Province_Alias"].ToString();
                txtProvince_Name.Text = dtProvince.Rows[0]["Province_Name"].ToString();
                txtProvince_Note.Text = dtProvince.Rows[0]["Province_Note"].ToString();
                cbDomain.SetValue(int.Parse(dtProvince.Rows[0]["Domain_Id"].ToString()));
            }
        }

        protected bool CheckInsert()
        {
            bool kiemtra = false;
            var clsProvince = new Province_BLL();
            DataTable dtProvince = clsProvince.BindGirdProvince(string.Empty);
            if (dtProvince != null && dtProvince.Rows.Count > 0)
            {
                foreach (DataRow dr in dtProvince.Rows)
                {
                    if (txtProvince_ID.Text != dr["Province_ID"].ToString())
                    {
                        if (txtProvince_Alias.Text == dr["Province_Alias"].ToString())
                        {
                            kiemtra = true;
                            clsFormat.Alert_ERROR("Thông Báo", "Mã tỉnh đã tồn tại !");
                        }
                        else if (txtProvince_Name.Text == dr["Province_Name"].ToString() &&
                                 cbDomain.SelectedItem.Value == dr["Domain_ID"].ToString())
                        {
                            kiemtra = true;
                            clsFormat.Alert_ERROR("Thông Báo", "Tên tỉnh thuộc miền này đã tồn tại !");
                        }
                    }
                }
            }
            return kiemtra;
        }

        protected void InsertProvince()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new Province_ET
                    {
                        Province_Alias = txtProvince_Alias.Text,
                        Province_Name = txtProvince_Name.Text,
                        Province_Note = txtProvince_Note.Text,
                        Domain_Id = int.Parse(cbDomain.SelectedItem.Value)
                    };
                    Province_BLL.InsertProvince(obj);
                    clsFormat.Alert_INFO("Thông Báo", "Thêm mới thành công !");
                    GetData();
                    GetComboBox();
                    ClearTextBox();
                }
            }
            catch (Exception ex)
            {
                clsFormat.Alert_ERROR("Lỗi", ex.ToString());
            }
        }

        protected void UpdateProvince()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new Province_ET
                    {
                        Province_Id = int.Parse(txtProvince_ID.Text),
                        Province_Alias = txtProvince_Alias.Text,
                        Province_Name = txtProvince_Name.Text,
                        Province_Note = txtProvince_Note.Text,
                        Domain_Id = int.Parse(cbDomain.SelectedItem.Value)
                    };
                    Province_BLL.UpdateProvince(obj);
                    WindowsProvince.Close();
                    clsFormat.Alert_INFO("Thông Báo", "Sữa thành công!");
                    GetData();
                }
            }
            catch (Exception ex)
            {
                clsFormat.Alert_ERROR("Lỗi", ex.ToString());
            }
        }

        private void Domain()
        {
            DataTable _dtDomainFillter = _clsDomain.BindGirdDomain(string.Empty);
            if (_dtDomainFillter != null && _dtDomainFillter.Rows.Count > 0)
            {
                StoreDomainProvince.DataSource = _dtDomainFillter;
                StoreDomainProvince.DataBind();
            }
            else
            {
                SelectBoxDomainProvince.GetStore().RemoveAll();
            }
        }
        #endregion

        #region [ComboBox]

        protected void GetComboBox()
        {
            var clsNational = new National_BLL();
            DataTable dtNational = clsNational.BindGirdNational(string.Empty);
            if (dtNational != null && dtNational.Rows.Count > 0)
            {
                NationalStore.DataSource = dtNational;
                NationalStore.DataBind();
            }

            var clsDomain = new Domain_BLL();
            DataTable dtDomain = clsDomain.BindGirdDomain(string.Empty);
            if (dtDomain != null && dtDomain.Rows.Count > 0)
            {
                DomainStore.DataSource = dtDomain;
                DomainStore.DataBind();
            }
        }

        protected void NationalSelectIndex_Change(object sender, StoreReadDataEventArgs e)
        {
            cbDomain.ReadOnly = false;
            var clsDomain = new Domain_BLL();
            if (cbNational.SelectedItem.Value != null)
            {
                DataTable dtDomain = clsDomain.BindGirdDomainNational(int.Parse(cbNational.SelectedItem.Value));
                if (dtDomain != null && dtDomain.Rows.Count > 0)
                {
                    DomainStore.DataSource = dtDomain;
                    DomainStore.DataBind();
                }
                else
                {
                    cbNational.Clear();
                    cbDomain.ReadOnly = true;
                    clsFormat.Alert_INFO("Thông báo", "Quốc gia này chưa có miền vui lòng chọn quốc gia khác khác !");
                }
            }
            else
            {
                cbNational.Clear();
                cbDomain.ReadOnly = true;
                clsFormat.Alert_INFO("Thông báo", "Vui lòng chọn quốc gia !");
            }
        }

        protected void ReloadComboBox(object sender, StoreReadDataEventArgs e)
        {
            GetComboBox();
        }

        #endregion

        #region[ClearTextbox]

        protected void ClearTextBox()
        {
            txtProvince_ID.Text = string.Empty;
            txtProvince_Alias.Text = MaTuDong();
            txtProvince_Name.Text = string.Empty;
            txtProvince_Note.Text = string.Empty;

            cbNational.Clear();
            cbDomain.Clear();
            cbDomain.ReadOnly = true;
        }

        #endregion

        #region[MaTangTuDong]

        protected string MaTuDong()
        {
            int i;
            DataTable dt = clsProvince.BindGirdProvince(string.Empty);
            if (dt != null && dt.Rows.Count > 0)
            {
                int max = int.Parse(dt.Rows[0]["Province_ID"].ToString());
                for (i = 0; i < dt.Rows.Count; i++)
                {
                    if (int.Parse(dt.Rows[i]["Province_ID"].ToString()) > max)
                    {
                        max = int.Parse(dt.Rows[i]["Province_ID"].ToString());
                    }
                }
                i = max + 1;
            }
            else
            {
                i = 0;
            }
            return "T_00" + i;
        }

        #endregion

        #region[Search]

        protected void Search_Event(object sender, DirectEventArgs e)
        {
            DataTable dtProvince = clsProvince.BindGirdProvince(txtSearch.Text);
            if (dtProvince != null && dtProvince.Rows.Count > 0)
            {
                StoreProvince.DataSource = dtProvince;
                StoreProvince.DataBind();
            }
        }

        #endregion

        #region[Permisstion]
        private void CheckPermisstion()
        {
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.Province, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnAdd == false)
                {
                    btnAddNew.Visible = false;
                }
                if (Is_BtnEdit == false)
                {
                    btnEdit.Visible = false;
                }
                if (Is_BtnView == false)
                {
                    PanelProvince.Visible = false;
                }
                if (Is_BtnDel == false)
                {
                    btnDel.Visible = false;
                }
                if (Is_BtnPrint == false)
                {
                    btnExport.Visible = false;
                }
            }
        }
        private bool CheckPermisstionDobclick()
        {
            var check = false;
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.Province, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnEdit == false)
                {
                    check = true;
                }
            }
            return check;
        }
        #endregion
        //----------Button-Windows---------------
    }
}